/*  Copyright (C) 2008  Alexander Blyzniuchenko (BlezAlex)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

using System.Collections;
using System.Collections.Generic;

namespace DC
{
    public static class HubConnectionManager
    {
        private static readonly Hashtable _connections = new Hashtable();
        private static string _myIp = "127.0.0.1";
        private static string _tempFolder;
        private static int lastConnectionID;

        public static void Initialize(string myIp, string tempFolder)
        {
            _myIp = myIp;
            _tempFolder = tempFolder;
        }

        public static HubClient CreateClient(string nickName)
        {
            HubClient c;
            lock (_connections)
            {
                c = new HubClient(nickName, _myIp, lastConnectionID, _tempFolder);
                _connections.Add(lastConnectionID, c);
                lastConnectionID++;
            }
            return c;
        }

        public static HubClient GetConnectedClient(int clientID)
        {
            lock (_connections)
            {
                HubClient c = (HubClient) _connections[clientID];
                if (c != null && c.IsConnected)
                    return c;
            }

            return null;
        }

        public static HubClient GetConnectedClient(string name)
        {
            lock (_connections)
            {
                foreach (HubClient c in _connections.Values)
                {
                    if (c != null && c.IsConnected && c.Name == name)
                        return c;
                }
            }

            return null;
        }

        public static List<HubClient> GetAliveConnections()
        {
            List<HubClient> clients = new List<HubClient>();
            foreach (DictionaryEntry e in _connections)
            {
                HubClient client = (HubClient)e.Value;
                if (client.IsConnected)
                    clients.Add(client);
            }
            return clients;
        }

        public static DcDirectory DcRoot
        {
            get
            {
                DcDirectory root = new DcDirectory(DcDirectory.PATH_SEPARATOR);
                foreach (HubClient c in GetAliveConnections())
                {
                    root.Directories.Add(c.Directory);
                }
                return root;
            }
        }
    }
}
